package com.ysg.widget.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.ysg.R;
import com.ysg.base.BaseDialog;
import com.ysg.enums.PayEnum;
import com.ysg.utils.YConfigUtil;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;

/**
 * 支付
 */
public class PayDialog extends BaseDialog {
    public interface OnPayListener{
        void onPay(PayEnum payEnum);
    }
    private OnPayListener listener;
    public void setOnPayListener(OnPayListener listener){
        this.listener = listener;
    }

    private String price;
    private TextView tvPrice;
    private ImageView ivClose;
    private View viewWechat, ViewAlipay;
    private CheckBox cbWechat, cbAlipay;
    private View viewConfirm;
    private PayEnum payEnum;
    public PayDialog(Context context, String price) {
        super(context);
        this.price = price;
    }


    @Override
    public int getLayoutId() {
        return R.layout.dialog_pay;
    }

    public void initView() {
        widthScale = 1f;

        ivClose = findViewById(R.id.ivClose);
        tvPrice = findViewById(R.id.tvPrice);
        viewWechat = findViewById(R.id.viewWechat);
        ViewAlipay = findViewById(R.id.ViewAlipay);
        cbWechat = findViewById(R.id.cbWechat);
        cbAlipay = findViewById(R.id.cbAlipay);
        viewConfirm = findViewById(R.id.tvConfirm);

        if (!YConfigUtil.isAlipaySwitch()){
            ViewAlipay.setVisibility(View.GONE);
        }
        if (!YConfigUtil.isWechatSwitch()){
            viewWechat.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initAnimation() {
        super.initAnimation();
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.dialog_animation);
    }

    @Override
    public void initData() {
        if (YStringUtil.isNotEmpty(price)){
            tvPrice.setText(getContext().getResources().getString(R.string.currency) + YStringUtil.removeZero(price));
        }
    }

    @Override
    public void initListener() {
        ivClose.setOnClickListener((v)->{
            dismiss();
        });
        viewConfirm.setOnClickListener((v)->{
            if (payEnum == null){
                YToastUtil.showShort(R.string.tip_pay_type);
                return;
            }
            dismiss();
            if (listener != null){
                listener.onPay(payEnum);
            }
        });
        viewWechat.setOnClickListener((v)->{
            cbWechat.setChecked(true);
            cbAlipay.setChecked(false);
            payEnum = PayEnum.wechat;
        });
        ViewAlipay.setOnClickListener((v)->{
            cbWechat.setChecked(false);
            cbAlipay.setChecked(true);
            payEnum = PayEnum.alipay;
        });
    }


}
